<template>

    <el-row type="flex" class="row-bg" justify="center">
        <el-col :xl="6" :lg="7">
            <h2>欢迎来到VueAdmin管理系统</h2>
            <el-image style="width: 180px; height: 180px" :src="require('@/assets/erweima.jpg')"></el-image>
            <p>公共号MarkerHub</p>
            <p>扫二维码，回复【VueAdmin】获取登陆密码</p>
        </el-col>
        <el-col :span="1">
            <el-divider direction="vertical"></el-divider>
        </el-col>
        <el-col :xl="6" :lg="7">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
                <el-form-item label="用户名:" prop="username">
                    <el-input v-model="loginForm.username" style="width:380px"></el-input>
                </el-form-item>
                <el-form-item label="密码:" prop="password">
                    <el-input v-model="loginForm.password" style="width:380px"></el-input>
                </el-form-item>
                <el-form-item label="验证码:" prop="code">
                    <el-input ref="refcode" v-model="loginForm.code" style="width:172px; float:left"></el-input>
                    <el-image class="captchaImg" :src="captchaImg"></el-image>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
                    <el-button @click="resetForm('loginForm')">重置</el-button>
              
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>

</template>
<script>
export default {

    data() {
        return {
            zhang: [],//账号密码
            tt: true,
            loginForm: {
                username: '',
                password: '',
                code: '',
                token: "",
                checknum: "",


            },
            rules: {
                tt: true,
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
                code: [
                    { required: true, message: '请输入验证码', trigger: 'blur' },

                    { min: 5, max: 5, message: '长度必须5个字符', trigger: 'blur' },
                ]
            },
            captchaImg: null
        };
    },
    created() {
        this.getCaptcha()
    },
    methods: {
        // 登录
       async submitForm(formName) {
           this.$refs[formName].validate((valid) => {
                if (valid) {

            
             this.$axios.post( "/login" ).then(res => {
                     const jwt = res.headers['aurhorization']
                this.$store.commit("SET_TOKEN", jwt)
                this.$router.push("/home")
            })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
       



        // submitForm(formName) {
        //     this.$refs[formName].validate((valid) => {
        //         if (valid) {
        //             if (this.loginForm.checknum == this.loginForm.code) {
        //                 axios({
        //                     url: '/login/getData',
        //                     method: 'get',
        //                     params: this.loginForm
        //                 }).then(res => {
        //                     console.log("挂载的值", res.data)
        //                     // const jwt = res.headers['aurhorization']

        //                     const jwt = "shuju"
        //                     sessionStorage.setItem("mytoken", jwt)
        //                     this.$store.commit("settoken", jwt)
        //                     this.$router.push("/")
        //                 })
        //             } else {
        //                 this.rules.code[0].message = '请重新输入验证码'
        //                 this.$refs.refcode.focus()
        //                 this.loginForm.code = ""
        //                 this.$refs.refcode.blur()
        //                 this.getCaptcha()
        //                 this.rules.code[0].message = '请输入验证码'
        //             }
        //         } else {
        //             console.log('error submit!!');
        //             return false;
        //         }
        //     });
        // },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        aaa() {
            this.rules.code[1].message = 'nihao'
            this.$forceUpdate()

        },
        //获取随机图片
         getCaptcha() {
             this.$axios.get( "/captcha" ).then(res => {
                this.captchaImg=res.data.data.captchaImg
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.el-row {
    height: 100%;
    background-color: #fafafa;
    display: flex;
    align-items: center;
    text-align: center;

    .el-divider {
        height: 200px;
    }

    .captchaImg {
        float: left;
        margin-left: 8px;
        border-radius: 4px;
    }

}
</style>